﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Transactions {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import Events.*;
    import Classes.*;
    import Classes.util.*;
    import Modules.workers.*;
    import Modules.bandits.*;
    import Modules.quest.Managers.*;
    import Display.*;
    import Modules.stats.experiments.*;
    import Modules.peddler.*;
    import Modules.stats.trackers.*;
    import Modules.stats.types.*;
    import Modules.achievements.*;
    import Init.PostInit.*;

    public class TInitUser extends TFarmTransaction {

        private const NUM_SEEN_EXPAND_FARM:int = 2;
        private const SHOW_EXPAND_FARM_SIZE:int = 14;
        private const SHOW_EXPAND_MIN_LEVEL:int = 4;

        protected var m_firstName:String;
        private var m_bookmarkCash:int = 0;

        public function TInitUser(_arg1:String){
            this.m_firstName = _arg1;
        }
        override public function perform():void{
            var _local1:String;
            _local1 = String(GlobalEngine.getFlashVar("unique_session_key"));
            signedCall("UserService.initUser", this.m_firstName, _local1);
        }
        override public function isInitTransaction():Boolean{
            return (true);
        }
        override protected function onComplete(_arg1:Object):void{
            if (_arg1 != null){
                if (((!((Global.world == null))) && (!((Global.player == null))))){
                    this.loadWorld(_arg1);
                } else {
                    ErrorManager.addError("InitUser global state is invalid", ErrorManager.ERROR_REMOTEOBJECT_FAULT);
                };
            } else {
                ErrorManager.addError("InitUser result is null", ErrorManager.ERROR_REMOTEOBJECT_FAULT);
            };
        }
        public function loadWorld(_arg1:Object):void{
            var _local3:Boolean;
            var _local4:Number;
            var _local5:Boolean;
            var _local6:Object;
            var _local7:Object;
            var _local8:Boolean;
            var _local9:Array;
            var _local10:int;
            var _local11:*;
            var _local12:Item;
            var _local13:String;
            var _local14:Array;
            var _local15:String;
            var _local16:String;
            var _local17:Number;
            var _local18:Number;
            var _local19:Number;
            var _local20:Number;
            var _local21:String;
            if (((((_arg1.userInfo) && (_arg1.userInfo.attr))) && (_arg1.userInfo.attr.debugTime))){
                Global.player.debugTime = _arg1.userInfo.attr.debugTime;
            };
            if (_arg1["flashHotParams"] != null){
                Global.flashHotParams = _arg1["flashHotParams"];
            };
            if (_arg1["eventTimeStamp"] != null){
                Global.player.currentTimestampEventID = _arg1["eventTimeStamp"].data.currentTimeStampID;
                Global.player.previousTimestampEventID = _arg1["eventTimeStamp"].data.previousTimeStampID;
            };
            var _local2:String;
            if (_arg1["overlaySurveyURL"] != null){
                _local2 = _arg1["overlaySurveyURL"];
            };
            if (((!((Global.world == null))) && (!((Global.player == null))))){
                LoadingManager.useAssetPacks = (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_ASSET_PACKS) == 1);
                LoadingManager.trackLoadTiming = true;
                LoadingManager.getInstance().addEventListener(LoaderEvent.ALL_TRACKED_LOADED, this.onTrackedLoaded);
                Global.pendingPresents = _arg1.pendingPresents;
                _local3 = _arg1.showBookmark;
                _local4 = -1;
                _local5 = _arg1.firstDay;
                _local6 = _arg1.copsAndBandits;
                
				// 搞明白franchiseManager是干嘛的
				Global.franchiseManager.loadObject(_arg1.franchises);
				
				// 搞明白crews是干嘛的
                Global.crews.loadObject(_arg1.crews);
				
				// 搞明白这个
                Global.factoryWorkerManager = new WorkerManager(WorkerManager.FEATURE_FACTORIES);
				
				// 搞明白这个
                Global.trainWorkerManager = new WorkerManager(WorkerManager.FEATURE_TRAINS);
				
                if (_arg1.featureData){
                    if (_arg1.featureData.factories){
                        Global.factoryWorkerManager.loadObject(_arg1.featureData.factories.workers);
                    };
                    if (_arg1["featureData"].trains){
                        Global.trainWorkerManager.loadObject(_arg1["featureData"]["trains"]["workers"]);
                    };
                };
				
				// 还有这个
                Global.copsNBanditsWorkerManager = new WorkerManager(WorkerManager.FEATURE_COPS_N_BANDITS);
				
                if (((_arg1.featureData) && (_arg1.featureData.copsNBandits))){
                    Global.copsNBanditsWorkerManager.loadObject(_arg1.featureData.copsNBandits.workers);
                };
                _local7 = _arg1.peddlers;
                if (((_arg1.featureData) && (_arg1.featureData.rollCall))){
                    Global.rollCallManager.loadObject(_arg1.featureData.rollCall);
                };
                if (_arg1.permissions){
                    GlobalEngine.socialNetwork.onPermissionDialogClosed((_arg1.permissions["publish_stream"] == "1"));
                };
                _arg1 = _arg1.userInfo;
                Global.player.cityName = _arg1.worldName;
                Global.player.pendingCityName = _arg1.pendingWorldName;
                if (Global.ui){
                    Global.ui.m_cityNamePanel.cityName = Global.player.cityName;
                };
                if (_arg1.player){
                    Global.player.loadObject(_arg1.player);
                    Global.player.newPlayer = _arg1.is_new;
                    Global.player.firstDay = _arg1.firstDay;
                    Global.player.creationTimestamp = _arg1["creationTimestamp"];
                };
                Global.gameSettings.setupSaleAndNewItems();
                if (_arg1["returnedFranchiseMoney"] != null){
                    _local10 = int(_arg1["returnedFranchiseMoney"]);
                };
                if (_local6){
                    Global.world.citySim.banditManager.parseServerData(_local6);
                };
                if (Global.isVisiting()){
                    UI.visitNeighbor(Global.getVisiting());
                } else {
                    _arg1.world.ownerId = GlobalEngine.socialNetwork.getLoggedInUserId();
					
					// 引擎的核心入口
                    Global.world.loadObject(_arg1.world);
					
                    Global.world.mostFrequentHelpers = _arg1.world.mostFrequentHelpers;
                    Global.world.vistorCenterText = (_arg1.world.visitorCenterMessage as String);
                    GlobalEngine.initializationManager.addEventListener(Event.COMPLETE, this.onInitComplete, false, 0, true);
                    Global.hud.applyConfig("tutorial");
                };
                if (_local6){
                    BanditUtil.refreshPoliceStationAppearance();
                    if (!Global.world.citySim.banditManager.areStationsLocked()){
                        BanditManager.spawnExistingCops();
                    };
                };
                Global.townName = Global.player.snUser.firstName;
                Global.hud.townName = (Global.townName = ZLoc.t("Main", "CityName", {name:ZLoc.tn(Global.townName)}));
                if (_arg1.player.licenses){
                    for (_local11 in _arg1.player.licenses) {
                        Global.player.addLicense(_local11, _arg1.player.licenses[_local11]);
                    };
                };
                _local8 = false;
                if (GlobalEngine.getFlashVar("frMsg") != null){
                    _local12 = null;
                    if (GlobalEngine.getFlashVar("frIconItem") != null){
                        _local12 = Global.gameSettings.getItemByName((GlobalEngine.getFlashVar("frIconItem") as String));
                    };
                    if (_local12){
                        _local13 = String(GlobalEngine.getFlashVar("frParams"));
                        _local14 = _local13.split(":");
                        _local15 = ZLoc.t((GlobalEngine.getFlashVar("frMsg") as String), null, _local14);
                        Global.postInitActions.add(new DoDisplayDialog(new GenericIconPopup(_local15, GenericPopup.TYPE_ACCEPTCANCEL, null, _local12.icon)));
                    } else {
                        Global.postInitActions.add(new DoDisplayDialog(new FriendRewardDialog()));
                    };
                } else {
                    if (GlobalEngine.getFlashVar("initMsg") != null){
                        _local16 = ZLoc.t((GlobalEngine.getFlashVar("initMsg") as String), null);
                        Global.postInitActions.add(new DoDisplayDialog(new GenericPopup(_local16)));
                    } else {
                        if (Global.isVisiting() == false){
                            _local17 = GlobalEngine.getTimer();
                            _local18 = (Global.gameSettings.getInt("hideGiftNotifyTimeout") * 1000);
                            _local19 = (((((_arg1.player) && (_arg1.player.options))) && (_arg1.player.options.noGiftNotify))) ? _arg1.player.options.noGiftNotify : 0;
                            _local20 = (_local17 - _local19);
                            if (((((((_arg1.player) && (_arg1.player.gifts))) && ((_arg1.player.gifts.length > 0)))) && ((_local20 >= _local18)))){
                                _local8 = true;
                            };
                        };
                    };
                };
                if (_local8){
                    Global.postInitActions.add(new DoDisplayDialog(new GiftNotify(_arg1.player.gifts.length)));
                };
                if (_arg1.bookmarkReward > 0){
                    this.m_bookmarkCash = _arg1.bookmarkReward;
                    _local21 = ZLoc.t("Dialogs", "BookmarkReward", {cash:this.m_bookmarkCash});
                    Global.postInitActions.add(new DoDisplayDialog(new GenericPopup(_local21, GenericPopup.TYPE_OK, this.onBookmarkReward)));
                } else {
                    if (((_local3) && ((_arg1.is_new == false)))){
                    };
                };
                Global.xpromoManager = new CrossPromoManager(GlobalEngine.getFlashVars());
                Global.xpromoManager.handleStartup();
                Global.achievementsManager = new AchievementManager();
                Global.achievementsManager.loadServerData(_arg1.player.achievements, null, false);
                Global.peddlerManager = new PeddlerGroupManager();
                Global.peddlerManager.loadObject(_local7);
                if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_POPULATION_TEASER) == ExperimentDefinitions.POPULATION_TEASER_SHOW){
                    Global.populationMilestoneManager = new PopulationMilestoneManager();
                };
                if ((((_arg1.player.motdVersion < Global.gameSettings.getInt("motdVersion"))) && ((_arg1.is_new == false)))){
                    Global.postInitActions.add(new DoDisplayDialog(new MOTD()));
                };
                _local9 = Global.player.getPlayerNews();
                if (_local9.length > 0){
                    GameTransactionManager.addTransaction(new TSawPlayerNews());
                };
                Global.questManager = new GameQuestManager();
                Global.player.setAllowQuests(false);
                Global.player.setCompletedQuests(_arg1.CompletedQuests);
                Global.world.viralMgr.loadActiveVirals(_arg1.player.activeVirals);
                Global.world.orderMgr.initializeOrders(_arg1.player.Orders);
                Global.world.citySim.lotManager.initLots();
                CloudManager.StartUp();
                if (_local2){
                    SurveyManager.instance.initializeSurveyOverlay(_local2);
                };
                Global.hud.conditionallyRefreshHUD();
                Global.freeGiftSaleManager.init();
                StartUpDialogManager.StartUpDialogTrigger(_arg1);
                LoadingManager.useAssetPacks = false;
                LoadingManager.trackLoadTiming = false;
                LoadingManager.worldLoaded = true;
            };
            dispatchEvent(new Event(Event.COMPLETE));
            Global.world.dispatchEvent(new FarmGameWorldEvent(FarmGameWorldEvent.USER_CHANGED));
        }
        private function onTrackedLoaded(_arg1:LoaderEvent):void{
            LoadingManager.getInstance().removeEventListener(LoaderEvent.ALL_TRACKED_LOADED, this.onTrackedLoaded);
            StartupSessionTracker.perf(StatsKingdomType.WORLDLOADED);
            var _local2:Number = _arg1.eventData["totalLoadTime"];
            var _local3:Number = _arg1.eventData["medianLoadTime"];
            var _local4:Number = _arg1.eventData["averageLoadTime"];
            StatsManager.sample(100, "asset_load_times", _local2.toString(), _local3.toString(), _local4.toString());
        }
        protected function parseNeighbors(_arg1:Array):void{
            UI.populateFriendBarData(_arg1);
            if (Global.ui){
                Global.ui.m_friendBar.populateNeighbors(Global.friendbar);
                Global.ui.setFriendBarPos(Math.max((Global.friendbar.length - 15), 0));
            };
            dispatchEvent(new Event(Event.COMPLETE));
        }
        protected function onBookmarkReward(_arg1:GenericPopupEvent):void{
            Global.player.gold = (Global.player.gold + this.m_bookmarkCash);
        }
        protected function onInitComplete(_arg1:Event):void{
            var _local2:MapResource;
            GlobalEngine.initializationManager.removeEventListener(Event.COMPLETE, this.onInitComplete);
            for each (_local2 in Global.world.getObjects()) {
                _local2.refreshArrow();
            };
        }

    }
}//package Transactions 
